﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using RBAC.API.Command.User;
using System.Transactions;

namespace RBAC.API.Handler.User
{
    public class UserDelHandler : IRequestHandler<UserDelCommand, ApiResult<int>>
    {
        private readonly IRepository<UserModel> userway;
        private readonly ILogger<UserLoginHandler> log;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;
        private readonly IRepository<RoleModel> roleway;
        private readonly IRepository<UserRoleModel> userroleway;
        private readonly IHttpContextAccessor httpContextAccessor;

        public UserDelHandler(IRepository<UserModel> userway, ILogger<UserLoginHandler> log,
            IMapper mapper, IConfiguration configuration, IRepository<RoleModel> roleway, IRepository<UserRoleModel> userroleway,
            IHttpContextAccessor httpContextAccessor)
        {
            this.userway = userway;
            this.log = log;
            this.mapper = mapper;
            this.configuration = configuration;
            this.roleway = roleway;
            this.userroleway = userroleway;
            this.httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UserDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            int i = 0;
            int l = 0;
            var user = await userway.GetValue(request.UserId);
            user.IsDel = true;
            var roles = userroleway.GetAll().Where(x => x.UserId == request.UserId&&x.IsDel==false).ToList();
            using (TransactionScope tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    i = await userway. Updateway(user);
                    foreach (var item in roles)
                    {
                        item.IsDel = true;
                        l += await userroleway.Updateway(item);
                    }
                    tran.Complete();  //提交
                }
                catch (Exception ex)
                {
                    log.LogInformation($"用户删除发生异常{ex.Message}");
                    //回滚
                    throw;
                }
            }
            if(i>0&&l>0)
            {
                log.LogInformation($"{DateTime.Now}{httpContextAccessor.HttpContext?.User.Claims?.FirstOrDefault(x=>x.Type=="UserName")?.Value??"默认值"}");
                res.Code = StateCode.OK;
                res.Message = "用户删除成功";
                res.Data = i + l;
                return res;
            }
            else
            {
                res.Code = StateCode.Fail;
                res.Message = "用户删除失败";
                return res;
            }
           

           
        }
    }
}
